#include "Socket.h"
#include "InetAddress.h"
#include "Logger.h"


#include <unistd.h>
#include <sys/socket.h>
#include <bits/types.h>
#include <strings.h>
#include <netinet/tcp.h>




Socket::~Socket(){
    ::close(sockfd_);
}

void Socket::bindAddress(const InetAddress &localAddr){


    if (::bind(sockfd_, (sockaddr*)localAddr.getSockAddr(), sizeof(sockaddr_in)) < 0) {
        LOG_FATAL("bind sockfd:%d fail", sockfd_);
    }
}
void Socket::listen(){
    if (::listen(sockfd_, 1024) < 0) {
        LOG_FATAL("listen sockfd:%d fail", sockfd_);
    }
}
int Socket::accept(InetAddress *peerAddr){
    sockaddr_in addr;
    bzero(&addr, sizeof addr);
    socklen_t len;
    int connfd = ::accept4(sockfd_, (sockaddr*)&addr, &len, SOCK_NONBLOCK | SOCK_CLOEXEC);
    if (connfd >= 0) {
        peerAddr->setSockAddr(addr);
    }
    return connfd;

}

void Socket::shutdownWrite(){
    if (::shutdown(sockfd_, SHUT_WR) < 0)
    {
        LOG_ERROR("shutdownWrite error");
    }
    
}

void Socket::setTcpNoDelay(bool on){
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval);
}
void Socket::setReuseAddr(bool on){
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
}
void Socket::setReusePort(bool on){
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEPORT ,&optval, sizeof optval);
}
void Socket::setKeepAlive(bool on){
    int optval = on ? 1 : 0;
    ::setsockopt(sockfd_, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof optval);
}